package day1102.demo03;

import day1031.demo02.MyRunnable;

import java.util.concurrent.*;

/**
 * @program: qy158
 * @description:
 * @author: 闫克起2
 * @create: 2022-11-02 10:17
 **/
public class Test {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        //创建一个Callable类型的任务对象
//        MyCallable my=new MyCallable();
//        //把任务返回的结果也封装到FutureTask对象中
//        FutureTask task=new FutureTask(my);
//        //创建一个线程对象，把上面的任务交于该线程对象。
//        Thread t=new Thread(task);
//
//        t.start();
//
//        System.out.println(task.get());
        ExecutorService executorService = Executors.newFixedThreadPool(2);

        Future<Integer> future = executorService.submit(new MyCallable());

        executorService.shutdown();
        System.out.println(future.get());
    }
}
//需要重写call方法，而且看到call方法有返回值。而且该方法有异常抛出
class MyCallable implements Callable<Integer>{


    @Override
    public Integer call() throws Exception {
        int sum=0;
        for(int i=1;i<=100;i++){
            sum+=i;
        }
        return sum;
    }
}
